﻿using ApplicationLibrary;
using ApplicationLibrary.SqlConnector;
using Fighter.Masters.ModuleData;

namespace Fighter.Fights.ModuleData
{

    #region RRFight

    public class RRFight : DataReaderUpdater
    {
        private SqlABParameter sqlP1;

        public RRFight()
            : base(false)
        {
        }

        protected override void CreateConnection()
        {
            myTable = FG_Fight.Name;

            QueryBuilder qb = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p1", FG_Fight.Active);

            qb.AddSelect();
            qb.AddFrom(FG_Fight.Name);
            qb.AddCompare(FG_Fight.Active, sqlP1);
            qb.AddOrder(FG_Fight.ID);

            sqlCM = new SqlABCommand(qb.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            sqlP1.Value = true;
        }
    }

    #endregion

    #region DUFight

    public class DUFight : DataReaderUpdater
    {
        private int fight;
        private SqlABParameter sqlP1;

        public DUFight()
            : base(true)
        {
        }

        protected override void CreateConnection()
        {
            myTable = FG_Fight.Name;

            QueryBuilder qb = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p1", FG_Fight.Active);

            qb.Update(FG_Fight.Name);
            qb.AddSet<bool>(FG_Fight.Active, false);
            qb.AddCompare(FG_Fight.ID, sqlP1, "!=");

            sqlCM = new SqlABCommand(qb.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        public bool Find(int fight)
        {
            this.fight = fight;
            return base.Find();
        }

        protected override void SetParameters()
        {
            sqlP1.Value = fight;
        }
    }

    #endregion


    #region DRFightActive

    public class DRFightActive : DataReaderUpdater
    {
        private SqlABParameter sqlP1;

        public DRFightActive()
            : base(false)
        {
        }

        protected override void CreateConnection()
        {
            myTable = FG_Fight.Name;

            QueryBuilder qb = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p1", FG_Fight.Active);

            qb.AddSelect();
            qb.AddFrom(FG_Fight.Name);
            qb.AddCompare(FG_Fight.Active, sqlP1);

            sqlCM = new SqlABCommand(qb.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            sqlP1.Value = true;
        }
    }

    #endregion

    #region RRRegistrated

    public class RRRegistrated : DataReaderUpdater
    {
        private SqlABParameter sqlP1;
        private bool present;

        public RRRegistrated()
            : base(false)
        {
        }

        protected override void CreateConnection()
        {
            myTable = FG_Registration.Name;

            QueryBuilder qb = new QueryBuilder(true);
            sqlP1 = new SqlABParameter("@p51", FG_Registration.Checked);

            qb.AddSelect();
            qb.AddSelect(FG_Club.Mail);
            qb.AddSelect(FG_Club.Club);
            qb.AddSelect(FG_Athlete.FirstName);
            qb.AddFrom(FG_Registration.Name);
            qb.AddFrom(FG_Club.Name);
            qb.AddFrom(FG_Athlete.Name);
            qb.AddFrom(FG_Fight.Name);
            qb.AddCompareColumn(FG_Registration.Mail, FG_Club.Mail);
            qb.AddCompareColumn(FG_Registration.Athlete, FG_Athlete.ID);
            qb.AddCompareColumn(FG_Registration.Fight, FG_Fight.ID);
            qb.AddCompare(FG_Registration.Checked, sqlP1);
            qb.AddCompare<bool>(FG_Fight.Active, true);
            qb.AddOrder(FG_Registration.Mail);
            qb.AddOrder(FG_Registration.Athlete);

            sqlCM = new SqlABCommand(qb.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            sqlP1.Value = present;
        }

        public bool Find(bool present)
        {
            this.present = present;
            return base.Find();
        }
    }

    #endregion

    #region DRRegistration

    public class DRRegistration : DataReaderUpdater
    {
        private SqlABParameter sqlP1, sqlP2, sqlP3;
        private string mail;
        private int athlete, fight;

        public DRRegistration(bool update)
            : base(update)
        {
        }

        protected override void CreateConnection()
        {
            myTable = FG_Registration.Name;

            QueryBuilder qb = new QueryBuilder();
            sqlP1 = new SqlABParameter("@p51", FG_Registration.Mail);
            sqlP2 = new SqlABParameter("@p52", FG_Registration.Athlete);
            sqlP3 = new SqlABParameter("@p53", FG_Registration.Fight);

            qb.AddSelect();
            qb.AddFrom(FG_Registration.Name);
            qb.AddCompare(FG_Registration.Mail, sqlP1);
            qb.AddCompare(FG_Registration.Athlete, sqlP2);
            qb.AddCompare(FG_Registration.Fight, sqlP3);

            sqlCM = new SqlABCommand(qb.Query, sqlCN);
            sqlCM.Parameters.Add(sqlP1);
            sqlCM.Parameters.Add(sqlP2);
            sqlCM.Parameters.Add(sqlP3);
            sqlDA = new SqlABDataAdapter(sqlCM);
        }

        protected override void SetParameters()
        {
            sqlP1.Value = mail;
            sqlP2.Value = athlete;
            sqlP3.Value = fight;
        }

        public bool Find(string mail, int athlete, int fight)
        {
            this.mail = mail;
            this.athlete = athlete;
            this.fight = fight;
            return base.Find();
        }
    }

    #endregion
}